package com.cxb.utils;

import com.cxb.entity.LocationPoint;
import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * Author jerry
 * date: 2016/7/29.
 */
public class GoogleWebServiceUtil {
	public static final String QUERY_LOCATION_POINT_URL = "https://maps.googleapis.com/maps/api/place/textsearch/json";
	public static final String QUERY_AUTO_COMPLETE_URL = "https://maps.googleapis.com/maps/api/place/queryautocomplete/json";
//	public static final String INPUT_AUTO_COMPLETE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/json";
	public static final String API_KEY = "AIzaSyDvyKRhA0kK75K35c55bb19M2KC9Mz2Ka0";
	public static final String KEY_STATUS = "status";
	public static final String STATUS_OK = "OK";
	public static final String ADDRESS_DESCRIPTION = "description";
	public static final String ADDRESS_PREDICTIONS = "predictions";
	public static final String ADDRESS_RESULTS = "results";
//	public static void main(String[] args) {
//		// write your code here
//		List<String> address = queryAutoComplete("梅林二村");
//		System.out.println(address.toString());
//
//		LocationPoint location = searchPlaceByAddress(address.get(0));
//		System.out.println(location);
//	}

	public static void searchLocationPointByAddress(String address, Callback.CommonCallback<String> callback){
		RequestParams requestParams = new RequestParams(QUERY_LOCATION_POINT_URL);
		requestParams.setMethod(HttpMethod.GET);
		requestParams.addBodyParameter("query", address);
		requestParams.addBodyParameter("key", API_KEY);
		x.http().get(requestParams, callback);
	}


	public static void queryAutoComplete(String address, Callback.CommonCallback<String> callback) {
		RequestParams requestParams = new RequestParams(QUERY_AUTO_COMPLETE_URL);
		requestParams.setMethod(HttpMethod.GET);
		requestParams.addBodyParameter("input", address);
		requestParams.addBodyParameter("key", API_KEY);
		x.http().get(requestParams, callback);
	}

	public static List<String> parseJsonForAddress(String json){
		try{
			JSONObject jsonObject = new JSONObject(json);
			String result = jsonObject.getString(KEY_STATUS);
			if(result.equals(STATUS_OK)){
				JSONArray jsonArray = jsonObject.getJSONArray(ADDRESS_PREDICTIONS);
				List<String> predictions = new ArrayList<String>();
				for (int i = 0; i < jsonArray.length(); i++){
					JSONObject object = jsonArray.getJSONObject(i);
					predictions.add(object.getString(ADDRESS_DESCRIPTION));
				}
				return predictions;
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public static LocationPoint parseJsonForLatAndLng(String json){
		try{
			JSONObject jsonObject = new JSONObject(json);
			String result = jsonObject.getString(KEY_STATUS);
			System.out.println(json);
			if(result.equals(STATUS_OK)){
				JSONArray jsonArray = jsonObject.getJSONArray(ADDRESS_RESULTS);
				JSONObject object = jsonArray.getJSONObject(0);
				JSONObject geometry = object.getJSONObject("geometry");
				JSONObject location = geometry.getJSONObject("location");

				LocationPoint point = new LocationPoint();
				point.latitude = location.getDouble("lat");
				point.longitude = location.getDouble("lng");
				return point;
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
}
